package com.meihua.shellapp.activity.personalcenter;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.meihua.shellapp.R;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.BaseBean;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;

import butterknife.Bind;

/**
 * Created by admin on 2017/9/29.
 */

public class ReleaseActivity extends BaseActivity {

    @Bind(R.id.iv_top_left)
    ImageView iv_top_left;
    @Bind(R.id.tv_title)
    TextView tv_title;
    @Bind(R.id.tv_top_right)
    TextView tv_top_right;
    @Bind(R.id.et_message)
    EditText et_message;//正文
    @Bind(R.id.et_title)
    EditText et_title;//标题

    @Override
    public int getLayoutId() {
        return R.layout.activity_release;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        iv_top_left.setVisibility(View.VISIBLE);
        iv_top_left.setOnClickListener(this);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getString(R.string.activity_release_title));
        tv_top_right.setVisibility(View.VISIBLE);
        tv_top_right.setText(getResources().getString(R.string.activity_release_submit));
        tv_top_right.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.tv_top_right://发表
                if (et_message.getText().toString().trim().equals("") || et_title.getText().toString().trim().equals("")) {
                    ToastUtils.getInstance().showToast("请填写内容");
                    return;
                }
                sendRelease(et_title.getText().toString(), et_message.getText().toString());
                break;
        }
    }

    private void sendRelease(String title, String message) {
        OkGo.<String>post(URL.getUrl().SENDRELEASE)
                .params("content", message)
                .params("title", title)
                .params("name", MyApplication.getInstance().bean.getName())
                .params("mid", MyApplication.getInstance().getMId())
                .execute(new MyStringCallBack<BaseBean>(tv_top_right) {

                    @Override
                    public void onSuccessData(BaseBean bean) {
                        super.onSuccessData(bean);
                        if (bean.getCode() == 1) {
                            finish();
                        }
                        ToastUtils.getInstance().showToast(bean.getMsg());
                    }
                });
    }
}
